package com.scs.application.core.pdf.json;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.scs.application.core.pdf.enums.PdfPageDirection;
import com.scs.application.core.pdf.enums.SpacingMode;
import com.scs.application.core.pdf.enums.TextShowType;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import javafx.scene.text.TextAlignment;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Type;

/**
 * @description:
 */
public class EnumSerializer implements ObjectSerializer {


    @Override
    public void write(JSONSerializer serializer, Object value, Object fieldName, Type fieldType, int features) throws IOException {
        if (fieldType == PdfPageDirection.class) {
            serializer.write(((PdfPageDirection) value).getCode());
            return;
        }
        if (fieldType == PageSize.class) {
            try {
                Field[] fields = PageSize.class.getFields();
                for (Field field : fields) {
                    PageSize pageSize = (PageSize) field.get(PageSize.class);
                    if (pageSize.equalsWithEpsilon((Rectangle) value) || pageSize.rotate().equalsWithEpsilon((Rectangle) value)) {
                        serializer.write(field.getName());
                        return;
                    }
                }
            } catch (IllegalAccessException e) {
                serializer.write("Default");
            }
            return;
        }

        if (fieldType == SpacingMode.class) {
            serializer.write(((SpacingMode) value).getCode());
            return;
        }
        if (fieldType == TextShowType.class) {
            serializer.write(((TextShowType) value).getCode());
            return;
        }

        if (fieldType == TextAlignment.class) {
            serializer.write(((TextAlignment) value).name());
            return;
        }

        /*serializer.write(((AuditStatus)object).getCode());*/
    }
}
